#!/bin/bash

if [ "$(id -u)" != "0" ]
then
  echo "Please run this with sudo or as user root" 2>&1
  echo "Since we need to install a couple of packages, sudo" 2>&1
  echo "actually IS needed here. Thanks :)" 2>&1
  exit 1
fi

pause() {
  read -n1 -r -p $'Press any key to continue or Ctrl+C to exit...\n' key
}

echo
echo "This will install the desktop environment on your Pi"
echo "Please keep in mind that the desktop environment needs"
echo "system resources that then might not be available for"
echo "printing, possible leading to print artifacts."
echo "It is not recommended to run the desktop environment"
echo "alongside OctoPrint if you do not have a Pi with"
echo "multiple cores (e.g. Pi1 or PiZero). Even then, use"
echo "at your own risk."
echo
echo "If you do not want to install the desktop environment"
echo "after all, please hit Ctrl+C now."
echo

pause

echo
echo "The desktop environment can be set up to start"
echo "automatically when the Pi boots."
echo "If you want to have it set up this way, please"
echo "type 'yes' now. Type 'no' if not."
echo -n "Finish with ENTER: "

read x_on_boot
[ "$x_on_boot" == "yes" ] || x_on_boot="no"

echo
echo "Going to install the desktop environment (automatic start on boot: $x_on_boot)"
echo
echo "This will take a while, do NOT switch off the Pi or close this console until done!"
echo

echo
echo "--- Updating our package list"
echo

apt-get update

echo
echo "--- Installing desktop packages"
echo

apt-get install --yes raspberrypi-ui-mods

if [ "$x_on_boot" == "yes" ]
then
  echo
  echo "--- Setting up Pi to boot to desktop"
  echo
  systemctl set-default graphical.target
else
  echo
  echo "--- Setting up Pi to not boot to desktop"
  echo
  systemctl set-default multi-user.target
fi

echo
echo "--- Done!"
echo

echo "You might want to reboot now: sudo reboot"
echo
